package com.xxx.dorm.service.impl;

import com.xxx.dorm.dao.BuildingDao;
import com.xxx.dorm.dao.DormitoryDao;
import com.xxx.dorm.dao.StudentDao;
import com.xxx.dorm.dao.impl.BuildingDaoImpl;
import com.xxx.dorm.dao.impl.DormitoryDaoImpl;
import com.xxx.dorm.dao.impl.StudentDaoImpl;
import com.xxx.dorm.domain.Building;
import com.xxx.dorm.domain.Dormitory;
import com.xxx.dorm.service.DormitoryService;

import java.util.List;

public class DormitoryServiceImpl implements DormitoryService {

    private DormitoryDao dormitoryDao = new DormitoryDaoImpl();
    private BuildingDao buildingDao = new BuildingDaoImpl();
    private StudentDao studentDao = new StudentDaoImpl();

    /**
     *  通过寝室号id查找寝室信息
     * @param did
     * @return
     */
    @Override
    public Dormitory findByDid(Integer did) {
        return dormitoryDao.findByDid(did);
    }

    @Override
    public List<Dormitory> findAll() {
        return dormitoryDao.findAll();
    }

    /**
     * 通过楼号名寻找所有的寝室号(修改和添加学生中用到)
     * @param buildName
     * @return
     */
    @Override
    public List<Dormitory> findDormByBuildName(String buildName) {
        return dormitoryDao.findDormByBuildName(buildName);
    }

    @Override
    public List<Dormitory> find(String conditions, String information) {
        return dormitoryDao.find(conditions,information);
    }

    @Override
    public void addDormitory(Dormitory dormitory) {
        dormitoryDao.addDormitory(dormitory);
        buildingDao.addCapablePeople(dormitory);
    }

    @Override
    public void updateDorm(Dormitory dormitory) {
        dormitoryDao.updateDorm(dormitory);
    }

    @Override
    public void deleteDorm(Integer id) {
        studentDao.emptyDid(id);
        Dormitory dormitory = dormitoryDao.findByDid(id);
        buildingDao.reduceCapablePeople(dormitory);
        dormitoryDao.deleteDormByBid(dormitory.getBid());
    }
}
